/*
    Description: process preload registry manager
 */
#include "preload_app_path_manager.h"
#include "log.h"
#include <string>
#include "performance/utils/perf_utils.h"

namespace PERF {

PreloadAppPathManager::PreloadAppPathManager()
{
    std::string userSid = PERF_UTILS.GetCurrentUserSid();
    if (userSid == "") {
        LOG_ERR("[PERF - PreloadAppPathManager] - get user sid failed");
        return;
    }
    std::string userSubKeyPath = userSid + REGISTRY_PATH;
    if (!REGISTRY_UTILS.CreateRegKey(HKEY_USERS, userSubKeyPath.c_str(), hKeyPreloadApp) || !hKeyPreloadApp) {
        LOG_ERR("[PERF - PreloadAppPathManager] - create registry key failed");
        return;
    }
}

PreloadAppPathManager::~PreloadAppPathManager()
{
    REGISTRY_UTILS.SafeCloseHKey(hKeyPreloadApp);
}

bool PreloadAppPathManager::GetPath(const std::string &pname, std::string &path)
{
    if (!hKeyPreloadApp) {
        LOG_ERR("[PERF - GetPath] - hkey is null!");
        return false;
    }
    if (!REGISTRY_UTILS.QueryRegSZ(hKeyPreloadApp, pname.c_str(), "path", path)) {
        LOG_ERR("[PERF - GetPath] - get app path failed");
        return false;
    }
    return true;
}

void PreloadAppPathManager::PutEntry(const std::string &pname, const std::string &path) {
    if (!hKeyPreloadApp) {
        LOG_ERR("[PERF - PutEntry] - hkey is null!");
        return;
    }
    REGISTRY_UTILS.SetRegSZ(hKeyPreloadApp, pname.c_str(), "path", path);
}

void PreloadAppPathManager::Remove(const std::string &pname) {
    if (!hKeyPreloadApp) {
        LOG_ERR("[PERF - Remove] - hkey is null!");
    }
    REGISTRY_UTILS.DeleteRegKey(hKeyPreloadApp, pname.c_str());
}

}  // namespace PERF
